import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:ball_lion/server/api/live_api.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/utils.dart';

import 'package:ball_lion/base/base_controller.dart';
import 'package:ball_lion/entity/live/expert_room_entity.dart';

class ExpertRoomsLogic extends BaseController {
  List<ExpertRoomEntity>? rooms;
  List<ExpertRoomEntity>? scheduleRoom;
  List<ExpertRoomEntity>? pastRoom;
  List<ExpertRoomEntity>? checkRoom;
  int _page = 1;

  int get page => _page;

  set page(int value) {
    _page = value;
    update();
  }

  @override
  void onReady() {
    getRefresh();
    super.onReady();
  }

  Future getData() async {
    rooms = await LiveApi.getExpertRooms(page, 15);
    divide();
    update();
  }

  void toLive(ExpertRoomEntity entity) {
    Get.toNamed(Routes.livePush, arguments: entity);
  }

  void divide({List<ExpertRoomEntity>? list}) {
    if (list.noValue) {
      checkRoom = [];
      scheduleRoom = [];
      pastRoom = [];
    }
    (list ?? rooms)?.forEach((element) {
      switch (element.status) {
        case -1:
          checkRoom?.add(element);
          break;
        case 0:
        case 1:
          scheduleRoom?.add(element);
          break;
        case 2:
          pastRoom?.add(element);
          break;
        default:
          break;
      }
    });
  }

  @override
  void getRefresh() async {
    page = 1;
    getData();
    update();
    super.getRefresh();
  }

  @override
  void loadMore() async {
    page++;
    var list = await LiveApi.getExpertRooms(page, 15);
    rooms?.addAll(list);
    divide(list: list);
    if ((rooms?.length ?? 0) < page * 15) {
      refreshController.finishLoad(IndicatorResult.noMore);
    } else {
      refreshController.finishLoad(IndicatorResult.success);
    }
    update();
    super.loadMore();
  }
}
